package dongbai.soft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import dongbai.soft.dao.OrderDao;
import dongbai.soft.entity.OrderEntity;
import dongbai.soft.feigncilent.ShedClient;
import dongbai.soft.service.OrderService;
import dongbai.soft.utils.PageUtils;
import dongbai.soft.utils.Query;
import dongbai.soft.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {

    @Autowired
    ShedClient shedClient;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Map<String, Object> filter = (Map<String, Object>) params.get("filter");//过滤器

        //前置变量和前置筛选
        Query<OrderEntity> orderEntityQuery = new Query<>();
        QueryWrapper<OrderEntity> orderQueryWrapper = new QueryWrapper<OrderEntity>().ge("price", filter.get("min_price"));
        if (!filter.get("which_date").equals("")) {
            try {
                orderQueryWrapper = orderQueryWrapper.between((String) filter.get("which_date")
                        , new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) filter.get("start_order_date"))
                        , new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) filter.get("end_order_date")));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        if (filter.get("max_price") != null) {
            orderQueryWrapper = orderQueryWrapper.le("price", filter.get("max_price"));
        }
        if (filter.get("isend") != null) {
            orderQueryWrapper = orderQueryWrapper.eq("isend", filter.get("isend"));
        }
        if (filter.get("status") != null) {
            orderQueryWrapper = orderQueryWrapper.eq("status", filter.get("status"));
        }
        if (filter.get("search") != null) {
            orderQueryWrapper = orderQueryWrapper.and(qw -> qw.like("cName", filter.get("search")).or().like("pName", filter.get("search")));
        }


        IPage<OrderEntity> page = null;

        int place = (int) filter.get("place");
        if (place == 0 || place == 1) {//调度中心和客服 看 所有
            page = this.page(
                    orderEntityQuery.getPage(params),
                    orderQueryWrapper
                    //all
            );

        } else if (place == 2) {//库房 看 待发货和待入仓库
            page = this.page(
                    orderEntityQuery.getPage(params),
                    orderQueryWrapper.and(orw -> orw.eq("isend", 0).eq("status", 2)
                            .or().eq("isend", 1).eq("status", 3))
                    //02 13
            );
        } else {
            orderQueryWrapper = orderQueryWrapper.eq("TRID", filter.get("TRID"));//对于分站，需要匹配该分站下的订单
            if (place == 3) {//分站 看 待入站
                page = this.page(
                        orderEntityQuery.getPage(params),
                        orderQueryWrapper.and(orw -> orw.eq("isend", 0).eq("status", 3)
                                .or().eq("isend", 1).eq("status", 5))
                        //03 15
                );

            } else if (place == 4) {//分站 看 待出站、待签收、退回库房
                page = this.page(
                        orderEntityQuery.getPage(params),
                        orderQueryWrapper.and(orw -> orw.eq("isend", 0).eq("status", 4)
                                .or().eq("isend", 0).eq("status", 5)
                                .or().eq("isend", 1).eq("status", 2))
                        //04 05 12
                );
            } else if (place == 5) {//分站 看 待结算订单
                page = this.page(
                        orderEntityQuery.getPage(params),
                        orderQueryWrapper.and(orw -> orw.eq("isend", 1).eq("status", 0).ge("deliver_price", 0)
                                .or().eq("isend", 1).eq("status", 4))
                        //10 14
                );
            }
        }


        return new PageUtils(page);
    }

    @Override
    public R queryCOrderList(List<String> orders) {
        List<OrderEntity> orderlist = new ArrayList<>();
        for (String ss :
                orders) {
            System.out.println(ss);
        }
        for (String oid : orders) {
            orderlist.add(this.getOne(new QueryWrapper<OrderEntity>().eq("OID", oid).select("OID", "pName", "price", "pNum", "order_date", "start_date",
                    "finish_date", "cancel_date", "dName", "deliver_price", "isend", "status")));


        }
        return R.ok().put("list", orderlist);
    }


    @Override
    public R cancelOrder(String oid, Integer status) {
        if (status == 0 || status == 1) {
            status = 0;
        } else if (status == 2) {
            status = 0;
            OrderEntity o = this.getById(oid);
            Map<String, Object> map = new HashMap<>();
            map.put("PID", o.getPid());
            map.put("num", o.getPnum());
            map.put("status", 3);
            shedClient.editnum(map);
        } else if (status == 3) {
            status = 5;
        } else {
            status = 2;
        }
        this.update(new UpdateWrapper<OrderEntity>().eq("OID", oid).set("isend", 1).set("status", status)
                .set("cancel_date", new Date()));
        return R.ok();
    }


    @Override
    public R getOneOrder(String oid) {
        OrderEntity orderEntity = this.getOne(new QueryWrapper<OrderEntity>().eq("OID", oid)
                .select("CID", "cName", "cPhone", "address", "PID", "pName", "price", "pNum", "TID", "ftName", "stName"));
        return R.ok().put("order", orderEntity);
    }


}